<script type="text/javascript">
$(document).ready(function(){
	$(".buy a").click(function(){
		$.ajax({
			url: "<?php echo $this->url(array("action" => "add"), "cart"); ?>",
			data: "pid="+ $(this).attr("href"),
			type: "POST",
			dataType: "html",
			success: function(data){
				if(confirm("Sản phẩm đã được cho vào giỏ hàng.\nBạn có muốn xem giỏ hàng của bạn không?"))
						location.href = "<?php echo $this->url(array("action" => "index"), "cart");?>"	;
			}
		});
		return false;
	});
	$("#popupContact a.redirect").click(function(){
		$("#popupContact").css("display", "none");
		$("#backgroundPopup").css("display", "none");
	});
	$("#popupContact a.close").click(function(){
		$("#popupContact").css("display", "none");
		$("#backgroundPopup").css("display", "none");
		return false;
	});
	$("#popupContact #backgroundPopup").click(function(){
		$("#popupContact").css("display", "none");
		$("#backgroundPopup").css("display", "none");
	});
	$("#frmSearch").submit(function(){
		var keyword = $("#keyword").attr("value");
		if( keyword == '' || keyword == "Tìm sản phẩm..."){
			alert("Bạn cần nhập từ khóa tìm kiếm!");
			return false;
		}
	});
	
	
});
</script>
<div id="main_content">
			<?php if(!isset($this->banners[3]->image) || $this->banners[3]->image !=''){?>
			<div id="popupContact">
				<a class="redirect" target="_blank" href="<?php echo $this->banners[3]->link;?>">
					<img src="<?php echo $this->banners[3]->image;?>">
				</a>
				<div id="popupContactClose">
					<a class="close" href="#">[x] Đóng lại</a>
				</div>
			</div>
			<div id="backgroundPopup"></div>
			<?php }?>
        	<div id="top">
            	<div id="sub_banner">
            		<?php Helper_HtmlRender::renderImageOrFlash($this->banners[1]->image, 723, 267, 'Thiết bị văn phòng Cường Phú')?>
            	</div>
                <div id="contact_banner">
                	<?php Helper_HtmlRender::renderImageOrFlash($this->banners[2]->image, 723, 76, 'Thiết bị văn phòng Cường Phú')?>
                </div>
            </div><!-- end top main_content block-->
            <?php
            	$id_arr = array();	
            	$id=1; 
            	$pcategoryModel = new Model_PCategory();
            	$productModel = new Model_Product();
				$indexPCategoryLv1 = 0;
				$countPCategoryLv1 = count($this->pCategorylv1); 
             foreach ($this->pCategorylv1 as $pcategorylv1){
             	$indexPCategoryLv1++;
             ?>
            <div class="products_box">
            	<div class="titles_products_box">
                	<div class="title_products_box"><?php echo $pcategorylv1->name?></div>
                    <div class="categories">
                    	<?php 
                    	try {
                    		$categorieslv2 = $pcategoryModel->getCategoryLv2ByIDLv1($pcategorylv1->id, 1);                 
                    		 $countCategorylv2 = count($categorieslv2);
                    		 for($i = 0; $i<$countCategorylv2; $i++){
                    	?>
                    	<div class="category <?php if($i==0){echo "displayed";} ?>" id="<?php  $id_arr[$i] = "category-".$id++; echo $id_arr[$i]; ?>">
							<div class="tab-left"></div>
							<div class="tab-mid"><?php echo $categorieslv2[$i]->name?></div>
							<div class="tab-right"></div>
						</div >
						<?php }}catch(Exception $ex){echo $ex->getMessage();} ?>
                    </div>
                </div>
                <?php
                 if(count($categorieslv2)!= 0){
                	for($i = 0; $i<$countCategorylv2; $i++){
                	?>
					<div class ="products_box_container <?php echo $id_arr[$i]; if($i==0){echo " displayed"; }?> ">
					<?php 
					try{
						$select = $productModel->select()->where('show_on_home = 1')->order("created_date DESC")->limit(4); 
						$products = $categorieslv2[$i]->findDependentRowset('Model_Product','Category',$select);
						if(count($products)!=0){	
							for($j=0;$j<count($products);$j++){
					?>
					<div class="product">
						<div class="image_product">
							<a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>">
							<?php 
	                		$arrImages = explode('@', $products[$j]->image);//để tách chuối ra thàng mảng mình sẽ lấy thằng ảnh đầu tiên
                                          if(extension_loaded("gd") && function_exists("exec") ){
                                          ?>
								<img src="<?php echo PUBLIC_URL.'/plugins/phpThumb/phpThumb.php?src='.$arrImages[0].'&amp;w=134&amp;h=99'?>" /> 	
							<?php }else{?>
								<img src="<?php echo $arrImages[0]?>" width="134px" height="99px" />
							<?php }?>
							</a>
						</div>
						<div class="model_product"><a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>"><?php echo $products[$j]->model; ?></a></div>
						<div class="decription_product"><?php echo $products[$j]->short_description ;?></div>
						<div class="price_product"><?php echo number_format($products[$j]->price);?> VND</div>
						<div class="action">
							<div class="buy"><a href="<?php echo $products[$j]->id?>">Mua</a></div>
							<div class="show_detail"><a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>">Chi tiết</a></div>
						</div>
					</div><!--end product block -->
					<?php }
					}else{
						echo "Chưa có sản phẩm nào";
					}
					}catch (Exception $ex)
						{
							echo $ex->getMessage();
						}?>
				</div>
				<?php }
                 }else {?>
					<div class="products_box_container displayed">
						<?php 
							try{  
								//lấy các sản phẩm thuộc category đang xét
								$select = $productModel->select()->where('show_on_home = 1')->order("created_date DESC")->limit(4);
								$products = $pcategorylv1->findDependentRowset('Model_Product','Category', $select);
								if(count($products)!=0){	//nếu có sản phẩm thì cho hiển thị tối đa 4 sản phẩm
									for($j=0;$j<count($products);$j++){
						?>
						<div class="product">	
							<div class="image_product">
								<a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>">
								<?php 
		                		$arrImages = explode('@', $products[$j]->image);//để tách chuối ra thàng mảng mình sẽ lấy thằng ảnh đầu tiên
		                		if(extension_loaded("gd") && function_exists("exec") ){ 
		                		?>
									<img src="<?php echo PUBLIC_URL.'/plugins/phpThumb/phpThumb.php?src='.$arrImages[0].'&amp;w=134&amp;h=99'?>" /> 	
								<?php }else{?>
									<img src="<?php echo $arrImages[0]?>" width="134px" height="99px" />
								<?php }?>
								</a>
							</div>
								<div class="model_product"><a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>"><?php echo $products[$j]->model; ?></a></div>
								<div class="decription_product"><?php echo $products[$j]->short_description ;?></div>
								<div class="price_product"><?php echo number_format($products[$j]->price);?> VND</div>
								<div class="action">
									<div class="buy"><a href="<?php echo $products[$j]->id?>">Mua</a></div>
							<div class="show_detail"><a href="<?php echo $this->url(array("slug" => $products[$j]->slug),"product");?>">Chi tiết</a></div>
								</div>
						</div><!--end product block -->
						<?php }
							}else{
								echo "Chưa có sản phẩm nào!";
							}
						}
						catch (Exception $ex){echo $ex->getMessage(); }?>
					</div>
				<?php }?>
				
                                                              
			</div><!--end products box-->
			<?php if($indexPCategoryLv1 < $countPCategoryLv1){?>
			<div class="space_block">
				<?php Helper_HtmlRender::renderImageOrFlash($pcategorylv1->ad_image, 719, 56, 'Thiết bị văn phòng Cường Phú')?>
			</div>
			<?php }
			}?>			
</div><!-- end main_content block -->

<div id="right">
        	<div id="search_box">
        		<form id="frmSearch" action="<?php echo $this->url(array(), "search");?>" method="get">
        			<input type="text" onclick="value='';"  id="keyword" name="keyword" value="Tìm sản phẩm..."/>
                	<input type="submit" id="submit_button" value="Tìm kiếm" />
        		</form>
            	
            </div><!-- end search_block-->
            <div id="cart_box">
            	<div id="title_cart"><a href="<?php echo $this->url(array(),"cart");?>">Giỏ hàng của bạn</a></div>
                <div class="cart_info">
                	<span class="left_info">Số sản phẩm:&nbsp; </span>
                    <span class="right_info"><?php echo $this->totalCartProduct;?></span>
                </div>
                <div class="cart_info">
                	<span class="left_info">Tổng tiền:&nbsp;</span>
                    <span class="right_info"><?php echo number_format($this->totalCartPrice);?> VND</span>
                </div>
            </div> <!-- end cart_block -->
            <?php
            //lấy dữ liệu để điền vào 2 box hỗ trợ trực tuyến
            $arrSalers = array();
            $arrTechnicians = array(); 
            foreach ($this->supporters as $supporter){
            	if($supporter->type == 0){
            		$arrSalers[] = $supporter;
            	}else{
            		$arrTechnicians[] = $supporter;
            	}
            }
            
                  
            ?>
            <div class="right_box">
            	<div class="title_box">
                	<div class="text_title_box">Bán hàng trực tuyến</div>
                    <div class="phone_title_box">(04) 35 68 36 99</div>
                </div>
                <?php $count_business =count($arrSalers);
               	if($count_business>0){
               	for( $i=0;$i<$count_business;$i++){?>
               	 <div class="supporter">
                	<div class="info_supporter">
                    	<div class="name_supporter">
                        	<a href="ymsgr:SendIM?<?php echo $arrSalers[$i]->yahoo_account;?>"  title="Contact to supporter!">
         						<?php echo $arrSalers[$i]->name;?>
                            </a>
                            </div>
                       	<div class="phone_supporter"><?php echo $arrSalers[$i]->phone;?></div>
                        <div class="email_supporter"><?php echo $arrSalers[$i]->email;?></div>
                        
                    </div>
                    <a href="ymsgr:SendIM?<?php echo $arrSalers[$i]->yahoo_account;?>"  title="Contact to supporter!">
                            <img  alt="" src="http://opi.yahoo.com/online?u=<?php echo $arrSalers[$i]->yahoo_account;?>&m=g&t=1" border="0"/>
                        </a>
                    <?php if($i+2 <= $count_business){?><div class="line"></div><?php } ?>
                </div>
               <?php }
               	 } ?>
                
               
            </div><!-- end right_box-->           
            <div class="right_box">
            	<div class="title_box">
                	<div class="text_title_box">Hỗ trợ kỹ thuật</div>
                    <div class="phone_title_box">(04) 66 84 87 86</div>
                </div>
                <?php $count_technical =count($arrTechnicians);
               	if($count_technical>0){
               	for( $i=0;$i<$count_technical;$i++){?>
               	 <div class="supporter">
                	<div class="info_supporter">
                    	<div class="name_supporter">
                        	<a href="ymsgr:SendIM?<?php echo $arrTechnicians[$i]->yahoo_account;?>"  title="Contact to supporter!">
         						<?php echo $arrTechnicians[$i]->name;?>
                            </a>
                            </div>
                        <div class="phone_supporter"><?php echo $arrTechnicians[$i]->phone;?></div>
                        <div class="email_supporter"><?php echo $arrTechnicians[$i]->email;?></div>
                        
                    </div>
                    <a href="ymsgr:SendIM?<?php echo $arrTechnicians[$i]->yahoo_account;?>"  title="Contact to supporter!">
                            <img  alt="" src="http://opi.yahoo.com/online?u=<?php echo $arrTechnicians[$i]->yahoo_account;?>&m=g&t=1" border="0"/>
                        </a>
                    <?php if($i+2 <= $count_technical){?><div class="line"></div><?php } ?>
                </div>
               <?php }
               	 } ?>
            </div><!-- end right_box-->
             <div class="right_box">
            	<div class="title_box">
                	<div class="text_title_box">Kênh tin tức</div>                    
                </div>
                <div class="news_box">
                <?php foreach($this->news as $news){	?>
                    <div class="news_link"><a href="<?php echo $this->url(array('slug'=>$news->slug),'post');?>"><?php echo $news->title;?></a></div>
                <?php }?>    
                    <div class="view_all"><a href="<?php echo $this->url(array('page'=> '','name' => Helper_Converter::stringToSlug($this->newsCategory->name), 'id' => $this->newsCategory->id), 'category');?>">Xem tất cả&gt;&gt;&gt;</a></div>               
                </div>
            </div><!-- end right_box --> 
            <div class="right_box">
            	<div class="title_box">
                	<div class="text_title_box">Kênh người dùng</div>
                </div>
                <div class="news_box">
               	<?php foreach($this->userNews as $userNews){	?>
                	<div class="news_link"><a href="<?php echo $this->url(array('slug'=>$userNews->slug),'post');?>"><?php echo $userNews->title;?></a></div>
                <?php }?>    
                    <div class="view_all"><a href="<?php echo $this->url(array('page'=> '','name' => Helper_Converter::stringToSlug($this->userNewsCategory->name), 'id' => $this->userNewsCategory->id), 'category');?>">Xem tất cả&gt;&gt;&gt;</a></div>                    
                </div>
            </div> <!-- end right_box -->
            <?php
            if(isset($this->banners[4])){
           	?>
            <div class="right_box">
            <?php 	
            	Helper_HtmlRender::renderImageOrFlash($this->banners[4]->image, 290, null, $this->banners[4]->link);
            ?>
            </div>
            <?php 
            }
             
            ?>
            <?php
            if(isset($this->banners[5])){
           	?>
            <div class="right_box">
            <?php 	
            	Helper_HtmlRender::renderImageOrFlash($this->banners[5]->image, 290, null, $this->banners[4]->link);
            ?>
            </div>
            <?php 
            }
             
            ?>
            <?php
            if(isset($this->banners[6])){
           	?>
            <div class="right_box">
            <?php 	
            	Helper_HtmlRender::renderImageOrFlash($this->banners[6]->image, 290, null, $this->banners[4]->link);
            ?>
            </div>
            <?php 
            }
            ?>
            <?php
            echo $this->partial("include/tags.phtml", array("tags" => $this->tags));
            ?>
            
</div> <!-- end right block --> 
<div id="clear"></div>
